package com.iefgames.cs.framework.support;

import java.security.MessageDigest;

public final class MD5 {

	private final static String[] hexDigits = {
	   "0", "1", "2", "3", "4", "5", "6", "7",
	   "8", "9", "a", "b", "c", "d", "e", "f"
	   };

	public static String byteArrayToHexString(byte[] b) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < b.length; ++i) {
			sb.append(byteToHexString(b[i]));
		}
		
		return sb.toString();
	}

	private static String byteToHexString(byte b) {
		int n = b;
		if (n < 0) {
			n = 256 + n;
		}
		
		int d1 = n / 16, d2 = n % 16;
		
		return hexDigits[d1] + hexDigits[d2];
	}

	public static String MD5Encode(String str) {
		String sb = null;

		try {
			sb = new String(str);
			MessageDigest md = MessageDigest.getInstance("MD5");
			sb = byteArrayToHexString(md.digest(sb.getBytes()));
		}
		catch (Exception ex) {}
		
		return sb;
	}
}
